/**
 * @file ethercat_example.c
 * @brief EtherCAT主站接口使用示例
 * 
 * 该文件演示了如何使用抽象接口同时兼容IgH和SOEM两种EtherCAT主站
 */

#include "ethercat_interface.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

/* 全局变量 */
static int running = 1;
static EtherCATInterface *ec_master = NULL;

/* 信号处理函数 */
void signal_handler(int sig) {
    running = 0;
}

/**
 * @brief 打印使用说明
 */
void print_usage(const char *program_name) {
    printf("Usage: %s <master_type> <ifname>\n", program_name);
    printf("  master_type: igh or soem\n");
    printf("  ifname: Network interface name (e.g. eth0)\n");
}

/**
 * @brief 主函数
 */
int main(int argc, char *argv[]) {
    if (argc < 3) {
        print_usage(argv[0]);
        return 1;
    }
    
    /* 解析命令行参数 */
    const char *master_type_str = argv[1];
    const char *ifname = argv[2];
    
    /* 确定主站类型 */
    EtherCATMasterType master_type;
    if (strcmp(master_type_str, "igh") == 0) {
        master_type = MASTER_TYPE_IGH;
    } else if (strcmp(master_type_str, "soem") == 0) {
        master_type = MASTER_TYPE_SOEM;
    } else {
        fprintf(stderr, "Unknown master type: %s\n", master_type_str);
        print_usage(argv[0]);
        return 1;
    }
    
    /* 注册信号处理函数 */
    signal(SIGINT, signal_handler);
    
    /* 创建EtherCAT主站实例 */
    ec_master = create_ethercat_master(master_type);
    if (!ec_master) {
        fprintf(stderr, "Failed to create EtherCAT master!\n");
        return 1;
    }
    
    /* 初始化EtherCAT主站 */
    if (!ec_master->init(ifname)) {
        fprintf(stderr, "Failed to initialize EtherCAT master on %s!\n", ifname);
        destroy_ethercat_master(ec_master);
        return 1;
    }
    
    /* 配置并自动发现从站 */
    int slave_count = ec_master->config_init();
    if (slave_count <= 0) {
        fprintf(stderr, "No slaves found!\n");
        ec_master->close();
        destroy_ethercat_master(ec_master);
        return 1;
    }
    
    /* 配置过程数据映射 */
    if (!ec_master->config_map(NULL)) {
        fprintf(stderr, "Failed to configure process data mapping!\n");
        ec_master->close();
        destroy_ethercat_master(ec_master);
        return 1;
    }
    
    /* 配置分布式时钟 */
    ec_master->config_dc();
    
    /* 等待所有从站达到SAFE_OP状态 */
    printf("Waiting for all slaves to reach SAFE_OP state...\n");
    ec_master->state_check(0, EC_STATE_SAFEOP, 500000);
    
    /* 请求所有从站进入OP状态 */
    printf("Request operational state for all slaves...\n");
    ec_master->write_state(0, EC_STATE_OPERATIONAL);
    
    /* 等待所有从站达到OP状态 */
    printf("Waiting for all slaves to reach OP state...\n");
    int timeout = 500000;
    if (!ec_master->state_check(0, EC_STATE_OPERATIONAL, timeout)) {
        fprintf(stderr, "Not all slaves reached operational state!\n");
        ec_master->read_state();
        ec_master->close();
        destroy_ethercat_master(ec_master);
        return 1;
    }
    
    printf("Operational state reached for all slaves.\n");
    
    /* 循环发送/接收过程数据 */
    printf("Starting cyclic operation. Press Ctrl+C to stop.\n");
    int cycle_counter = 0;
    while (running) {
        /* 发送过程数据 */
        ec_master->send_processdata();
        
        /* 接收过程数据 */
        int wkc = ec_master->receive_processdata(1000);
        
        /* 每1000个周期打印一次信息 */
        if (cycle_counter % 1000 == 0) {
            printf("Cycle %d, WKC: %d\n", cycle_counter, wkc);
        }
        
        cycle_counter++;
        
        /* 简单延时 */
        usleep(1000);
    }
    
    printf("\nRequesting init state for all slaves...\n");
    ec_master->write_state(0, EC_STATE_INIT);
    
    /* 关闭EtherCAT主站 */
    printf("Closing EtherCAT master...\n");
    ec_master->close();
    destroy_ethercat_master(ec_master);
    
    return 0;
}